<template>
  <div class="top">
    <div class="left">
      <span class="left-btn" @click="goHome">首页</span>
    </div>
    <div class="center">
      <div class="title">智慧旅游可视化大数据展示平台</div>
    </div>
    <div class="right">
      <span class="right-btn" @click="fullScreen">
        {{ flag? '全屏' : '退出全屏' }}
      </span>
      <span class="time">当前时间：{{ time }}</span>
    </div>
  </div>
</template>

<script setup lang="ts" name="Top">
import moment from 'moment'
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

// 存储当前时间
let time = ref(moment().format('YYYY-MM-DD HH:mm:ss'))
// 存储定时器
let timer = ref()
let flag = ref(true)

// 获取路由器对象
const router = useRouter()
// 点击首页按钮返回首页的回调
const goHome = () => {
  router.push('/home')
}
// 点击全屏按钮的回调
const fullScreen = () => {
  let full = document.fullscreenElement
  if (!full) {
    // 切换为全屏
    document.documentElement.requestFullscreen()
    flag.value = false
  } else {
    // 退出全屏
    document.exitFullscreen()
    flag.value = true
  }
}
// 当组件挂载时开启定时器更新时间
onMounted(() => {
  timer.value = setInterval(() => {
    time.value = moment().format('YYYY-MM-DD HH:mm:ss')
  }, 1000)
})
// 当前组件销毁前关闭定时器
onBeforeUnmount(() => {
  // 清除定时器
  clearInterval(timer.value)
})
</script>

<style scoped lang="scss">
.top {
  width: 100%;
  height: 40px;
  display: flex;
  .left {
    flex: 1;
    background: url(../../images/dataScreen-header-left-bg.png) no-repeat;
    background-size: cover;
    .left-btn {
      width: 150px;
      height: 40px;
      float: right;
      background: url(../../images/dataScreen-header-btn-bg-l.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px; /* 行高和高度相同可以垂直居中 */
      color: #29fcff;
      font-size: 20px;
      &:hover {
        cursor: pointer;
      }
    }
  }
  .center {
    flex: 2;
    .title {
      width: 100%;
      height: 74px;
      background: url(../../images/dataScreen-header-center-bg.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 74px;
      color: #29fcff;
      font-size: 35px;
    }
  }
  .right {
    flex: 1;
    background: url(../../images/dataScreen-header-right-bg.png) no-repeat;
    background-size: cover;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .right-btn {
      width: 150px;
      height: 40px;
      background: url(../../images/dataScreen-header-btn-bg-r.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px; /* 行高和高度相同可以垂直居中 */
      color: #29fcff;
      font-size: 20px;
      &:hover {
        cursor: pointer;
      }
    }
    .time {
      color: #29fcff;
      font-size: 18px;
      margin-right: 10px;
    }
  }
}
</style>
